package com.ruoyi.project.system.material.img.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.material.img.domain.MaterialImg;
import com.ruoyi.project.system.material.img.service.IMaterialImgService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 素材中心图片Controller
 * 
 * @author LCL
 * @date 2021-01-21
 */
@Controller
@RequestMapping("/system/materialimg")
public class MaterialImgController extends BaseController
{
    private String prefix = "system/materialimg";

    @Autowired
    private IMaterialImgService materialImgService;

    @RequiresPermissions("system:img:view")
    @GetMapping()
    public String img()
    {
        return prefix + "/img";
    }

    /**
     * 查询素材中心图片列表
     */
    @RequiresPermissions("system:img:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MaterialImg materialImg)
    {
        startPage();
        List<MaterialImg> list = materialImgService.selectMaterialImgList(materialImg);
        return getDataTable(list);
    }

    /**
     * 导出素材中心图片列表
     */
    @RequiresPermissions("system:img:export")
    @Log(title = "素材中心图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MaterialImg materialImg)
    {
        List<MaterialImg> list = materialImgService.selectMaterialImgList(materialImg);
        ExcelUtil<MaterialImg> util = new ExcelUtil<MaterialImg>(MaterialImg.class);
        return util.exportExcel(list, "img");
    }

    /**
     * 新增素材中心图片
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存素材中心图片
     */
    @RequiresPermissions("system:img:add")
    @Log(title = "素材中心图片", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MaterialImg materialImg)
    {
        return toAjax(materialImgService.insertMaterialImg(materialImg));
    }

    /**
     * 修改素材中心图片
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        MaterialImg materialImg = materialImgService.selectMaterialImgById(id);
        mmap.put("materialImg", materialImg);
        return prefix + "/edit";
    }

    /**
     * 修改保存素材中心图片
     */
    @RequiresPermissions("system:img:edit")
    @Log(title = "素材中心图片", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MaterialImg materialImg)
    {
        return toAjax(materialImgService.updateMaterialImg(materialImg));
    }

    /**
     * 删除素材中心图片
     */
    @RequiresPermissions("system:img:remove")
    @Log(title = "素材中心图片", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(materialImgService.deleteMaterialImgByIds(ids));
    }
}
